$.Class("Adv.Grid",
// static properties
{       	
    addParams : function(e, params)
    {
        e.sender.transport.options.read.data = params;
    }    
},
// prototype properties
    {
        // constructor function
        init: function(id) {
            this.id = id;
            
            this.dataSource = this.createDataSource();
            this.createGrid();     
            this.initEvents(id);
        },
        createDataSource: function()
        {
            var dataSource = new kendo.data.DataSource({
                transport: {
                    read: {
                        url: "/adv/search",
                        dataType: "json",
                        type: "post",
                        data : {
                            
                        }
                    },
					update: {
						url: "/budget/update",
						dataType: "json",
						type: "post",
					},
					create: {
						url: "/budget/create",
						dataType: "json",
						type: "post"						
					},
					destroy: {
						url: "/budget/delete",
						dataType: "json",
						type: "post"
					},
					parameterMap: function(options, operation) {
						if (operation == "create" || operation == "update"){
							var model = options.models[0];
							model.hyipId = $("#ddlHyip").val();							
							model.monitorId = $("#ddlMonitor").val();
							
							if($("#startDate").val() == "") delete(model.startDate);
							else model.startDate = $("#startDate").val();
							if($("#endDate").val() == "") delete(model.endDate);	
							else model.endDate = $("#endDate").val();
							
							delete(model.grid_index);                                                    
							delete(model.domain);
							delete(model.monitor);
							delete(model.hyip);
						}
						
						return options;
					}
                },
				change: function (e, data) {	
					if(e.action == "sync")
						$("#grid").data("kendoGrid").dataSource.read();	
				},
				error: function (xhr, error) {
					//console.debug(xhr); console.debug(error);
				},				
				batch: true,
                pageSize: 25,
                serverPaging: true,
                serverFiltering: true,
                serverSorting: true,
                schema: {
                    data: "results",
                    total: function(data) {
                        return data.total;
                    },
					model: {
						id: "id",
						fields: {							
							cost: { validation: { required: true }},
							description: {},	
						}
					}
                },							
                requestStart: function(e) {  							
					var data = {                        
                        isAjax : true,
						monitor : $("#_ddlMonitor").val(),
                        hyip : $("#_ddlHyip").val(),
                        startDate : $("#_startDate").val(),
                        endDate : $("#_endDate").val()
                    };
                    
                    Adv.Grid.addParams(e, data);				
                }
            });

            return dataSource;
        }, 
        createGrid : function()
        {
            $("#" + this.id).kendoGrid({
	        dataSource: this.dataSource,
	        toolbar: ["create","Reset"],
	        //height: 500,
	        filterable: false,	        
	        pageable: true,
	        columns: [{
                field:"grid_index",
                title:" ",
                width: 40
            },{
                field:"monitor",
                title:"Monitor",
                width: 200                
            },{
                field:"hyip",
                title:"Hyip",
                width: 170                
            },{
                field:"cost",
                title:"Cost",
                width: 100,
                template: '$${cost}'
            },{
                field:"startDate",
                title:"Start date",
                width: 100
            },{
                field:"endDate",
                title:"End date",
                width: 100
            },{
                field:"description",
                title:"Description",
                width: 300                
            },{ 
                command: ["edit","destroy"], title: "", width: 170 
            },{
            	title:"",
                field:"",   
                template: ''
            }],
            editable: {
                mode: "popup",
                template: $("#budgetTemplate").html(),
                update: true,
                destroy: true,				
                confirmation: "Are you sure you want to remove this item ?",
                window:{
                        width:650,
                        center: true
                }
            },
            edit: function(e)
            {		
                var monitors = $("#_ddlMonitor").data("kendoComboBox").dataSource;                
                
                $("#ddlMonitor").kendoComboBox({
                    index: 0,
                    placeholder: "Select monitor",
                    dataTextField: "domain",
                    dataValueField: "id",
                    filter: "contains",
                    dataSource: monitors
                });
                
				var hyips = $("#_ddlHyip").data("kendoComboBox").dataSource;                
                
                $("#ddlHyip").kendoComboBox({
                    index: 0,
                    placeholder: "Select hyip",
                    dataTextField: "domain",
                    dataValueField: "id",
                    filter: "contains",
                    dataSource: hyips
                });
            },
            save: function(e) {                
                e.model.dirty = true;
            },
            sortable:true,
            selectable: 'row',
            dataBound: function(e) {                
                
            }
        });	
        },
        initEvents : function(id)
        {           
            $(".k-grid-Reset", "#" + this.id).click(function(){
                $("#" + id).data("kendoGrid").dataSource.read();	// Reset grid
                return false;
            });	
        },
        refresh : function()
        {
            $("#" + this.id).data("kendoGrid").dataSource.read();	// Reset grid
        }
})